package buildcmd

import (
	"gitee.com/godY/daminghu-cli-server/common"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"os/exec"
)

func BuildServer(serverpath string, projectname string) {
	logs.Info("Start BuildServer")
	cmdStr := "cp -R " + serverpath + " $GOPATH/src/gitee.com/godY/ && cd $GOPATH/src/gitee.com/godY/" + projectname + " && gofmt -w . && cd $GOPATH/src/gitee.com/godY/" + projectname + "/cmd  && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o " + projectname + "-linux main.go && CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -o " + projectname + "-win.exe main.go  && CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o " + projectname + "-mac main.go && zip -q -r " + projectname + ".zip ./conf ./sql " + projectname + "* && cp " + projectname + ".zip " + common.DOWNLOAD_FILEPATH
	logs.Info("Start BuildServer Cmd => ", cmdStr)
	cmd := exec.Command("bash", "-c", cmdStr)
	//cmd := exec.Command("bash", "-c", "daminghu-cli -build all -cfg ./temp/cfg.json -mode dev -jsondata ./temp/data.json")
	//cmd := exec.Command("daminghu-cli","-build web")
	cmdOut, err := cmd.Output()
	if err != nil {
		logs.Error(err, cmdOut)
		panic(err)
	}

	logs.Info("> " + cmdStr)
	logs.Info(">> "+string(cmdOut))
	logs.Info("End BuildServer")
}

func BuildWeb(webpath string, projectname string) {
	logs.Info("Start buildWeb")
	cmdStr := "cd " + webpath + " && npm install && npm run build -dd && zip -q -r " + projectname + ".zip ./dist && cp " + projectname + ".zip " + common.DOWNLOAD_FILEPATH
	logs.Info("Start buildWeb Cmd => ", cmdStr)
	cmd := exec.Command("bash", "-c", cmdStr)
	//cmd := exec.Command("bash", "-c", "daminghu-cli -build all -cfg ./temp/cfg.json -mode dev -jsondata ./temp/data.json")
	//cmd := exec.Command("daminghu-cli","-build web")
	cmdOut, err := cmd.Output()
	if err != nil {
		logs.Error(err, cmdOut)
		panic(err)
	}
	logs.Info("> " + cmdStr)
	logs.Info(">> "+string(cmdOut))
	logs.Info("End buildWeb")
}

func RunDaminghuCliCmd(build string, cfgpath string, jsondatapath string) {
	logs.Info("Start daminghu-cli")
	beego.Debug(build, cfgpath, jsondatapath)
	cmdStr := "daminghu-cli -build " + build + " -cfg " + cfgpath + " -mode dev -jsondata " + jsondatapath
	logs.Info("Start daminghu-cli Cmd => ", cmdStr)
	cmd := exec.Command("bash", "-c", cmdStr)
	//cmd := exec.Command("bash", "-c", "daminghu-cli -build all -cfg ./temp/cfg.json -mode dev -jsondata ./temp/data.json")
	//cmd := exec.Command("daminghu-cli","-build web")
	cmdOut, err := cmd.Output()
	if err != nil {
		logs.Error(err, cmdOut)
		panic(err)
	}
	logs.Info("> daminghu-cli")
	logs.Info(string(cmdOut))
	logs.Info("End daminghu-cli")
}
